/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ZeroCopy_Client;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
 

//I used the following namespace only for writing the final result into a text file
import java.io.*;
import java.net.SocketAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 *
 * @author Teva
 */
public class ZeroCopyClient 
{

    private static long start;
    private static long end;
    private static String Filepath;
    private static boolean Blocking = true; 
    
    private static SocketChannel sc = null;
    private static FileChannel fc = null;
      
    
    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException 
    {
        //ZeroCopyClient.SendFile("/Users/teva/Desktop/3.csv");
        
     
        writeResult("ZeroCopy Approach Testing");
        writeResult("Configuration Blocking " + Blocking);
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");
        Date date = new Date();
        writeResult("Test Date  " + dateFormat.format(date));
        writeResult("Included: [Prefetching.]");
        writeResult("================================");
        
       int j;       
        
       int k;
             
         
        for(j=11;j<12;j++) //this loop is used to crawl 10 files
        {
           StringBuilder FP = new StringBuilder("/Users/teva/Desktop/DataFile/File").append(j).append(".txt"); 
           
           Filepath = FP.toString();
           
           Prefecthing(Filepath);
           
            for(k=1;k<6;k++)
            { 
                 writeResult("Iteration No=> " + k + " AND File name is=> File" +j +  " Total time => " + SendFile(Filepath)); 
                 
            }       
         }
        
         
    }
    
    public static long SendFile(String FilePath)
    {
     long timeDifference;
     File input = new File(FilePath);     
     try
      {
            System.out.println("Client ZeroCopy initiating..."); 
            SocketAddress sad = new InetSocketAddress("10.141.11.125",3289); 
            sc = SocketChannel.open();            
            sc.connect(sad);          
            sc.configureBlocking(Blocking);  
             
            System.out.println("Client channel connected");            
                        
            System.out.print("Set Socket Buffer Size: " + sc.socket().getSendBufferSize());
            
            fc = new FileInputStream(input).getChannel(); 
            
            
           long position = 0;
           
           start = System.currentTimeMillis();
              fc.transferTo(position, fc.size(), sc);
           end = System.currentTimeMillis();
          
          timeDifference = end-start;
          
          System.out.println("Network ZeroCopy transfer time from client side = "+timeDifference+"ms");
          sc.close();
          fc.close(); 
          return timeDifference;
      }
      catch (IOException e) 
      {
            return -1;
      }     
    }
     
    public static void Prefecthing(String FilePath) throws FileNotFoundException, IOException 
    {
        
        FileChannel fc1 = null;
        
        fc1 = new FileInputStream(FilePath).getChannel();
        
        ByteBuffer bb = ByteBuffer.allocate(1024);
        
        int len=0;
        
        while((len=fc1.read(bb))>=0) 
        {
          bb.clear();
        }
       
    }
    
    
    public static void writeResult(String Results)
     {
    try 
    {
        FileWriter outFile = new FileWriter("/Users/teva/Desktop/ZeroCopyResult.txt",true);
        PrintWriter out = new PrintWriter(outFile);      
        out.println(Results);        
        out.close();
   } 
    catch(IOException e)
    { 
    }
    
     }
    
   

}
